<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="cn.firstsoft.firstframe.admin.PropertyFile"%>
<%@ page import="cn.firstsoft.firstframe.admin.XmlFile"%>
<%@ page import="cn.firstsoft.firstframe.admin.Field"%>
<%@ page import="cn.firstsoft.firstframe.admin.InputValue"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<%@ include file="../common.jsp"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    
    <title>设置</title>
   
	<link href="../style.css" type="text/css" rel=stylesheet>
	<script language="javascript" src="../Check.js"></script>
	<script language="javascript" src="../../scripts/admin.js"></script>
  </head>
  
  <body onload="hidePanle()">
<%
	
	String method = request.getParameter("method")==null?"add":request.getParameter("method");
	Field field = null;
	String nameValues = "" , inputValues="";
	if(method.equals("edit"))
	{
		
		String pname = request.getParameter("pname");
		field = XmlFile.findByPname(pname);
		
		List inputValueList = field.getInputValues();
		if(inputValueList != null)
		{
			for(int i=0;i<inputValueList.size();i++)
			{
				InputValue iv = (InputValue)inputValueList.get(i);			
				nameValues += iv.getName() + ",";
				inputValues += iv.getValue() + ",";
			}
			nameValues = nameValues.endsWith(",")?nameValues.substring(0,nameValues.length()-1):nameValues;
			inputValues = inputValues.endsWith(",")?inputValues.substring(0,inputValues.length()-1):inputValues;
		}
	}
%>

<P/>

<c:set var="flag" value="${param.flag}" />
<c:if test="${not empty flag}">
	<c:if test="${flag=='exists'}"> 
		属性名 ${param.pname} 已存在，属性名必须唯一!
	</c:if>
	<c:if test="${flag=='suc'}">
		设置保存成功！

	</c:if>
	<c:if test="${flag=='err'}">
		设置保存失败！

	</c:if>
</c:if>

<c:set var="nameValues" value="<%=nameValues%>" />
<c:set var="inputValues" value="<%=inputValues%>" />

<c:set var="field" value="<%=field%>" />
<FORM name="fieldform" action="field_edit.jsp" method="post" onsubmit="return checkForm()">
<input class=button type=button value="提&nbsp;&nbsp;交" onclick="checkForm();"/>
<input class=button type=reset value="重&nbsp;&nbsp;置" />
	<table width="100%" align="center">
  		<TR>
			  <TH class=listViewPaginationTdS1  align=left colSpan=4>设置</TH>
		</TR>
	<!--	<TR>
			<td width="30%" class=dataLabel align="right">类型所属：*</td>
			
				<TD width="70%" align="left">
					<INPUT type=text name="typeCname" size=20 value="${field.typeCname}" />
				</TD>
			
		</TR>
	-->
		<TR>
			<td width="30%" class=dataLabel align="right">类型：*</td>
			<TD width="70%" align="left">
				<SELECT name="type">
					<OPTION value="workflow,工作流" <c:if test="${field.type=='workflow'}">selected="selected"</c:if> >工作流 (workflow)</OPTION>
					<OPTION value="mail,邮件" <c:if test="${field.type=='mail'}">selected="selected"</c:if> >邮件 (mail)</OPTION>
					<OPTION value="message,消息" <c:if test="${field.type=='message'}">selected="selected"</c:if> >消息 (message)</OPTION>
					<OPTION value="other,其他" <c:if test="${field.type=='other'}">selected="selected"</c:if> >其他 (other)</OPTION>
				</SELECT>		
			</TD>
		</TR>
		<TR>
			<td width="30%" class=dataLabel align="right">属性名：*</td>
			
			<td width="70%" align="left">
			<c:if test="${param.method=='add'}">
				<input type=text name="pname" value="${field.pname}" size=40 />
			</c:if>
			<c:if test="${param.method=='edit'}">
				<input type="hidden" name="pname" value="${field.pname}" />
				<b>${field.pname}</b>
			</c:if>
			</td>
		</TR>
		<TR>
			<td width="30%" class=dataLabel align="right">属性中文名：*</td>
			<td width="70%" align="left">
				<input type=text name="cname" value="${field.cname}" size=40  />
			</td>
		</TR>
		<TR>
			<td width="30%" class=dataLabel align="right">input字段名：*</td>
			<td width="70%" align="left">
				<input type=text name="name" value="${field.name}" size=40 />
			</td>
		</TR>
		<TR>
			<td width="30%" class=dataLabel align="right">input字段类型：</td>
			<td width="70%" align="left">
				<SELECT name="inputtype" onchange="hidePanle()">
					<OPTION value="text" <c:if test="${field.inputType=='text'}">selected="selected"</c:if> >文本框(text)</OPTION>
					<OPTION value="select" <c:if test="${field.inputType=='select'}">selected="selected"</c:if> >下拉列表(select)</OPTION>
					<OPTION value="radio" <c:if test="${field.inputType=='radio'}">selected="selected"</c:if> >单选框(radio)</OPTION>
				</SELECT>				
			</td>
		</TR>
		
				<tr id="inputLength">
					<td width="30%" class=dataLabel align="right">文本框输入长度：*</td>
					<td width="70%" align="left">
						<INPUT type=text name="inputSize" value="${field.inputSize}" size=10 />
					</td>	
				</tr>		
		
		
			<tr id="nameValuesTR">
				<td width="30%" class=dataLabel align="right">字段显示值：*</td>
				<td width="50%" align="left">				
					<INPUT type=text name="valueName" value="${nameValues}" size=40/>多个值用逗号隔开
				</td>			
			</tr>	
			
			<tr id="inputValuesTR" >
				<td width="30%" class=dataLabel align="right">字段值：*</td>
				<td width="50%" align="left">				
					<INPUT type=text name="inputValues" value="${inputValues}" size=40 />多个值用逗号隔开
				</td>			
			</tr>	
		
			<tr>
				<td width="30%" class=dataLabel align="right">默认值：</td>
				<td width="50%" align="left">				
					<INPUT type=text name="defaultValue" value="${field.defaultValue}" size=30 />
				</td>			
			</tr>	
			<tr>
				<td width="30%" class=dataLabel align="right">排序号：*</td>
				<td width="50%" align="left">				
					<INPUT type=text name="pos" value="${field.pos}" size=10 />
				</td>			
			</tr>	
			<tr>
				<td width="30%" class=dataLabel align="right">描述：</td>
				<td width="50%" align="left">				
					<INPUT type=text name="desc" value="${field.desc}" size=70 />
				</td>			
			</tr>		
			<tr>
				<td width="30%" class=dataLabel align="right">验证提示字段：</td>
				<td width="50%" align="left">				
					<INPUT type=text name="alertField" value="${field.validatorAlertField}" size=20 />
				</td>		
			</tr>		
			<tr>
				<td width="30%" class=dataLabel align="right">验证规则：</td>
				<td width="50%" align="left">				
					<INPUT type="checkbox" name="require" value=required="true" size=20 onclick="addToTextArea('require')"/>非空
					<INPUT type="checkbox" name="isNum" value=check="isInt('0+')" size=20 onclick="addToTextArea('isNum')"/>正整数

				</td>		
			</tr>	
			<TR>
				<td width="30%" class=dataLabel align="right">&nbsp;</td>
				<td width="50%" align="left">													
					<TEXTAREA name="rules" cols="80" rows="5">${fn:trim(field.inputValidator)}</TEXTAREA>								
				</td>					
			</TR>
			<tr>
				<td>&nbsp;</td>
				<td>
					<a href="validatorRulesInfo.jsp?openwin=true" target="_blank">规则设置详细方法</a>
				</td>
			</tr>
	</table>

</FORM>
   
  </body>
<script language="javascript">
	function addToTextArea(str)
	{
		if(str=='require')
		{
			if(document.all.require.checked)
			{
				var requireValue = document.all.require.value;
				var value = document.all.rules.value;
				
				value += ' '+requireValue + ' ';
				document.all.rules.value = value;
			}else
			{
				var requireValue = document.all.require.value;
				var value = document.all.rules.value;			
				value = value.replace(" "+requireValue,"");
				document.all.rules.value = trim(value);
			}
		}else{		
			if(document.all.isNum.checked)
			{
			
				var isNum = document.all.isNum.value;
				var value = document.all.rules.value;
				
				value += ' '+isNum + ' ';
				document.all.rules.value = value;
			}else
			{
				var isNum = document.all.isNum.value;
				var value = document.all.rules.value;			
				value = value.replace(" "+isNum,"");
				document.all.rules.value = trim(value);
			}
		}	
		
	}
	function trim(str)
	{
	    if(str == null) return "" ;
	
	    // 去除前面所有的空格
	    while( str.charAt(0)  == ' ' )
	    {
	        str = str.substring(1,str.length);
	    }
	
	    // 去除后面的空格

	    while( str.charAt(str.length-1)  == ' ' )
	    {
	        str = str.substring(0,str.length-1);
	    }
	
	    return str ;
	}
	
	function hidePanle(str)
	{
		var value = document.all.inputtype.value;
		if(value=='text')
		{
			document.getElementById('inputLength').style.display='';
			document.getElementById('nameValuesTR').style.display='none';
			document.getElementById('inputValuesTR').style.display='none';
		}
		else
		{			
			document.getElementById('inputLength').style.display='none'	
			document.getElementById('nameValuesTR').style.display='';
			document.getElementById('inputValuesTR').style.display='';
		}		
		}
	function checkForm()
	{
	
		var value = document.all.inputtype.value;						
		var name = document.all.name.value;
		var pname = document.all.pname.value;	
		var cname = document.all.cname.value;
		var inputSize = document.all.inputSize.value;
		var valueName = document.all.valueName.value;
		var inputValues = document.all.inputValues.value;
		var pos = document.all.pos.value;
		
		
		if(getParameter('method')=='add')
		{
			
			if(pname=='')
			{
				alert('属性名 不能为空！');
				document.all.pname.focus();
				return false;
			}
		}
		if(cname=='')
		{
			alert('属性中文名 不能为空！');
			document.all.cname.focus();
			return false;
		}
		if(name=='')
		{
			alert('input字段名 不能为空！');
			document.all.name.focus();
			return false;
		}
		
		if(value=='select' || value=='radio')
		{
			
			if(valueName=='')
			{
				alert('字段显示值 不能为空！');
				document.all.valueName.focus();
				return false;
			}
			if(inputValues=='')
			{
				alert('字段值 不能为空！');
				document.all.inputValues.focus();
				return false;
			}
			
			var nameLength = valueName.split(",");
			var inputValueLength = inputValues.split(",");			
			if(nameLength.length != inputValueLength.length)
			{
				alert('字段显示值的个数和字段值的个数不对应！');
				document.all.valueName.focus();
				return false;
			}
		}else
		{				
			if(inputSize=='')
			{
				alert('文本框输入长度 不能为空！');
				document.all.inputSize.focus();
				return false;
			}
			if(!isDig(inputSize))
			{
				alert('文本框输入长度 必须为正整数！');
				document.all.inputSize.focus();
				return false;
			}
		}
		if(pos=='')
		{
			alert('排序号 不能为空！');
			document.all.pos.focus();
			return false;
		}
		if(!isDig(pos))
		{
			alert('排序号 必须为正整数！');
			document.all.pos.focus();
			return false;
		}
		document.fieldform.action = 'field_update.jsp?method='+getParameter('method')+'&pname='+pname;
		document.fieldform.submit();
	}
	
	function getParameter(name)
	{
	    var js_get =self.window.document.location.href;
	    var start    =js_get.indexOf(name + '=');
	    if (start == -1) return '';
	    var len = start + name.length + 1;
	    var end = js_get.indexOf('&',len);
	    if (end == -1) end = js_get.length;
	    return unescape(js_get.substring(len,end));
	}
	function isDig(s){ 
		var re = /^[0-9]{1,20}$/;
	    return re.exec(s)
	}
	
</script>
</html>
